package filewriting;

import java.io.*;
import java.util.ArrayList;
import guiobjects.Observation;


import user.data.Data;

/**
 * This class contains a method that creates a file that contains observations of several 
 * simulated human beings.
 * 
 * @author Trevor Terris
 *
 */
public class ObservationsWriter
{
    /**
     * This method creates the file of observations.
     * 
     * @throws IOException
     */
    public static Thread startWriting(ArrayList<Observation> observationList, String dirName)
    {
    	Thread toReturn = new WritingThread(observationList, dirName);
    	toReturn.start();
    	return toReturn;
    }
    private static class WritingThread extends Thread
    {
    	private ArrayList<Observation> myObservationList;
    	private String myDirName;
    	public WritingThread(ArrayList<Observation> observationList, String dirName)
    	{
    		myObservationList = observationList;
    		myDirName = dirName;
    	}
    	public void run()
    	{
    		try
        	{ 
    	        File file = new File(myDirName + "/" + Data.RAW_DATA_NAME);
    	        //A new BufferedWriter is created
    	        Writer output = null;
    	        output = new BufferedWriter(new FileWriter(file));
    	        output.flush();
    	        output.write(Data.observationsFramerate + "\n");
    	        for (int i = 0; i < myObservationList.size(); i++)
    	        	output.write(myObservationList.get(i).toString());
    	        output.close();
        	}
        	catch (Exception e)
        	{
        		System.out.println("Error writing to file.");
        	}
    	}
    }
}